#include "system.h"

static void fldrv_fan_ctrl_init()
{
	gpio_init_type gpio_init_struct;
	tmr_output_config_type tmr_oc_init_structure;
	tmr_brkdt_config_type tmr_brkdt_config_struct;
	
	crm_periph_clock_enable(CRM_TMR17_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
	
  gpio_default_para_init(&gpio_init_struct);
  gpio_init_struct.gpio_pins = GPIO_PINS_7 | GPIO_PINS_9;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init(GPIOB, &gpio_init_struct);

  gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_2);	//要用到PWM互补就要两个一起设置
	gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE9, GPIO_MUX_2);
	
	//prescaler_value = (uint16_t)(system_core_clock / 25000) - 1;
	/* tmr17 time base configuration */
  tmr_base_init(TMR17, 3850,0);
  tmr_cnt_dir_set(TMR17, TMR_COUNT_UP);

  tmr_output_default_para_init(&tmr_oc_init_structure);
  tmr_oc_init_structure.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
  tmr_oc_init_structure.oc_output_state = TRUE;
  tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
  tmr_oc_init_structure.oc_idle_state = TRUE;
	tmr_oc_init_structure.occ_output_state = TRUE;
  tmr_oc_init_structure.occ_polarity = TMR_OUTPUT_ACTIVE_LOW;
  tmr_oc_init_structure.occ_idle_state = FALSE;
	
  tmr_output_channel_config(TMR17, TMR_SELECT_CHANNEL_1, &tmr_oc_init_structure);
  tmr_channel_value_set(TMR17, TMR_SELECT_CHANNEL_1, 1925);
	
	 /* automatic output enable, stop, dead time and lock configuration */
  tmr_brkdt_default_para_init(&tmr_brkdt_config_struct);
  tmr_brkdt_config_struct.brk_enable = TRUE;
  tmr_brkdt_config_struct.auto_output_enable = TRUE;
  tmr_brkdt_config_struct.deadtime = 11;
  tmr_brkdt_config_struct.fcsodis_state = TRUE;
  tmr_brkdt_config_struct.fcsoen_state = TRUE;
  tmr_brkdt_config_struct.brk_polarity = TMR_BRK_INPUT_ACTIVE_HIGH;
  tmr_brkdt_config_struct.wp_level = TMR_WP_LEVEL_3;
  tmr_brkdt_config(TMR17, &tmr_brkdt_config_struct);

  /* output enable */
  tmr_output_enable(TMR17, TRUE);
	/* tmr enable counter */
  tmr_counter_enable(TMR17, TRUE);
}

static void fldrv_fan_read_init()
{
	gpio_init_type gpio_init_struct;
	tmr_input_config_type  tmr_input_config_struct;
 /* enable tmr16/gpioa clock */
  crm_periph_clock_enable(CRM_TMR16_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
	
  /* timer3 input pin Configuration */
  gpio_init_struct.gpio_pins = GPIO_PINS_8;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init(GPIOB, &gpio_init_struct);

  gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE8, GPIO_MUX_2);

	 /* tmr3 counter mode configuration */
  tmr_base_init(TMR16, 0xFFFF, 0);
  tmr_cnt_dir_set(TMR16, TMR_COUNT_UP);
  /* tmr3 configuration: input capture mode
     the external signal is connected to tmr3 ch2 pin (pa.07)
     the rising edge is used as active edge,
     the tmr3 c2dt is used to compute the frequency value */

	tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_1;
  tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
  tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE;
	tmr_input_channel_init(TMR16, &tmr_input_config_struct, TMR_CHANNEL_INPUT_DIV_1);

  tmr_interrupt_enable(TMR16, TMR_C1_INT, TRUE);

  /* tmr2 trigger interrupt nvic init */
  nvic_irq_enable(TMR16_GLOBAL_IRQn, 0, 2);

  /* enable tmr3 */
  tmr_counter_enable(TMR16, TRUE);
}

void fldrv_pwm_init(void)
{
	fldrv_fan_ctrl_init();
	fldrv_fan_read_init();
}



